package com.coderli.shurnim.stock.biz.data.impl;

import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coderli.shurnim.stock.biz.data.CommonDataInput;
import com.coderli.shurnim.stock.biz.data.StockDataInput;
import com.coderli.shurnim.stock.model.StockFundFlow;
import com.coderli.shurnim.stock.model.StockRealTimeData;

/**
 * 从腾讯股票网址获取数据实现类 <br>
 * 主要使用到的网址：http://qt.gtimg.cn/q=
 * 
 * @author OneCoder
 * @date 2013年8月19日 下午10:28:27
 * @website http://www.coderli.com
 */
public class TecentDataInput extends CommonDataInput implements StockDataInput {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(TecentDataInput.class);
	private static final String URL_PREFIX = "http://qt.gtimg.cn/q=";
	private static final String URL_FUND_FLOW_PREFIX = "http://qt.gtimg.cn/q=ff_";
	private static final Charset TECENT_RESP_CHARSET = Charset.forName("gbk");
	private static final String SPLIT_STR = "~";
	private static final int INDEX_NAME = 1;
	private static final int INDEX_CODE = 2;
	private static final int INDEX_CURRENT_VALUE = 3;
	private static final int INDEX_Y_CLOSE = 4;
	private static final int INDEX_T_OPEN = 5;
	private static final int INDEX_TRADE_VOLUME = 6;
	private static final int INDEX_VOLUME_MONEY = 37;
	private static final int INDEX_PRICE_CHANGE = 31;
	private static final int INDEX_CHANGE_RANGE = 32;
	private static final int INDEX_TURNOVER_RATE = 38;
	private static final int INDEX_MAX_PRICE = 41;
	private static final int INDEX_MIN_PRICE = 42;
	private static final int INDEX_TOTAL_MARKET_VALUE = 45;
	private static final int INDEX_CIRCUL_MARKET_VALUE = 44;

	@Override
	public StockRealTimeData getData(String code) {
		LOGGER.info("Get data of stock: {}", code);
		String dataInfo = getInfoStr(code, URL_PREFIX, TECENT_RESP_CHARSET);
		if (dataInfo != null) {
			StockRealTimeData srtd = new StockRealTimeData();
			parseDataStr(dataInfo, srtd);
			return srtd;
		}
		return null;
	}

	@Override
	public StockFundFlow getFundFlow(String code) {
		LOGGER.info("Get fund flow of stock: {}", code);
		String fundFlowStr = getInfoStr(code, URL_FUND_FLOW_PREFIX,
				TECENT_RESP_CHARSET);
		if (fundFlowStr != null) {
			StockFundFlow sff = new StockFundFlow();
			sff.setCode(code);
			parseFundStr(fundFlowStr, sff);
			return sff;
		}
		return null;
	}

	private void parseFundStr(String fundFlowStr, StockFundFlow sff) {
		
	}

	/*
	 * 解析请求获得的股票字符串信息
	 */
	private void parseDataStr(String dataInfo, StockRealTimeData strd) {
		String infos[] = dataInfo.split(SPLIT_STR);
		strd.setName(infos[INDEX_NAME]);
		strd.setCode(infos[INDEX_CODE]);
		strd.setYesterdayClosingPrice(Double.valueOf(infos[INDEX_Y_CLOSE]));
		strd.setOpeningPrice(Double.valueOf(infos[INDEX_T_OPEN]));
		strd.setPrice(Double.valueOf(infos[INDEX_CURRENT_VALUE]));
		// 成交量
		strd.setTradingVolume(Integer.valueOf(infos[INDEX_TRADE_VOLUME]));
		// 成交额
		strd.setVolume(Integer.valueOf(infos[INDEX_VOLUME_MONEY]));
		strd.setChangePrice(Double.valueOf(infos[INDEX_PRICE_CHANGE]));
		strd.setChangeRange(Double.valueOf(infos[INDEX_CHANGE_RANGE]));
		strd.setMaxPrice(Double.valueOf(infos[INDEX_MAX_PRICE]));
		strd.setMinPrice(Double.valueOf(infos[INDEX_MIN_PRICE]));
		strd.setCirculationMarketValue(Long
				.valueOf(infos[INDEX_CIRCUL_MARKET_VALUE]));
		strd.setTotalMarketValue(Long.valueOf(infos[INDEX_TOTAL_MARKET_VALUE]));
		strd.setTurnoverRate(Double.valueOf(infos[INDEX_TURNOVER_RATE]));
	}
}
